<?php
include 'functions.php';

$sql = "SELECT user_id FROM ych_user WHERE type_id!=2";
$kefu = db_fetch_arrays($sql, $conn);

$tmpArr = array();
foreach($kefu as $v){
	$tmpArr[] = $v['user_id'];
}
$end = strtotime('2013-08-07 09:00:00');
$sql = "SELECT p1.user_id,p1.username,p1.invite_userid,p1.invite_money FROM ych_user AS p1 
		LEFT JOIN ych_borrow_tender AS p2 ON p1.user_id=p2.user_id
		LEFT JOIN ych_borrow AS p3 ON p2.borrow_id=p3.id 
		WHERE p3.fstatus=3 AND p3.ftype!=5 AND p1.invite_userid>0 AND p1.invite_money=0 AND p1.add_time<{$end}
		GROUP BY p1.user_id 
		HAVING(SUM(p2.account)>=10000)";
$res = db_fetch_arrays($sql, $conn);

foreach ($res as $info){
	if(isset($info['user_id'])){
		$sql = "UPDATE ych_user SET invite_money='20' WHERE user_id='{$info['user_id']}'";
		mysql_query($sql);
		
		if( !in_array($info['invite_userid'], $tmpArr) ){//客服或内部人员推荐不增加实际金额
			$account_result_reward 	= db_fetch_array("select * from ych_account where user_id={$info['invite_userid']}", $conn);
			$log['user_id'] 		= $info['invite_userid'];
			$log['ftype'] 			= 43;
			$log['money'] 			= 20;
			$log['total'] 			= $account_result_reward['total']+$log['money'];
			$log['use_money'] 		= $account_result_reward['use_money']+$log['money'];
			$log['no_use_money'] 	= $account_result_reward['no_use_money'];
			$log['collection'] 		= $account_result_reward['collection'];
			$log['to_user'] 		= "1";
			$log['remark'] 			= "推荐奖励";
			$log['add_time'] 		= time();
			$log['addip'] 			= '127.0.0.1';
			$mode = $info['invite_userid']%100;
			$sql  ="insert into ych_account_log_$mode(user_id,ftype,total,money,use_money,no_use_money,
															collection,to_user,remark,add_time,addip) values( 
						 {$info['invite_userid']},{$log['ftype']},{$log['total']},{$log['money']},
						 {$log['use_money']},{$log['no_use_money']},
						 {$log['collection']},{$log['to_user']},'{$log['remark']}',
						 {$log['add_time']},'{$log['addip']}'
						 )";
			mysql_query($sql, $conn);
			mysql_query("update ych_account set total=total+{$log['money']},
						 use_money=use_money+{$log['money']} where user_id={$info['invite_userid']}", $conn);
		}
	}
}
echo "over";
?>
